#include "ParticleFactory.h"


/*static*/ ParticleFactory * ParticleFactory::GetInstance(void)
{
	static ParticleFactory Instance;
	return &Instance;
}

ParticleFactory::ParticleFactory(void)
{
	
}

ParticleFactory::~ParticleFactory(void)
{

}

void ParticleFactory::Enter()
{
	if(loadEmitter("testEmitter.xml") == false)
	{
		Emitter temp;
		char tempname[20] = {"example"};
		strcpy_s(temp.mapName, 20, tempname);
		temp.posY				= 100;
		temp.posX				= 100;
		temp.height				= 100;
		temp.width				= 500;
		temp.emitterShape		= RECTANGLE;
		temp.spread				= 10;
		temp.spawnRate		= 1;
		temp.direction			= 1.0f;
		temp.AlphaStart		= 255;
		temp.RcolorStart		= 255;
		temp.GcolorStart		= 255;
		temp.BcolorStart		= 255;
		temp.AlphaEnd			= 255;
		temp.RcolorEnd		= 255;
		temp.GcolorEnd		= 30;
		temp.BcolorEnd		= 30;
		temp.scaleStart			= 1.0f;
		temp.scaleEnd			= 3.0f;
		temp.velocityYStart	= 300.0f;
		temp.velocityXStart	= 100.0f;
		temp.velocityYEnd	= -1000.0f; 
		temp.velocityXEnd	= 2000.0f;
		temp.lifeMin				= 0.1f;
		temp.lifeMax				= 1.5f;
		temp.rotationStart		= 0.0f;
		temp.rotationRate		= 1.0f;
		temp.maxParticles		= 1000;
		temp.looping				= true;

		string tempName = temp.mapName;
		emitterMap.insert(std::pair<string, Emitter>(tempName, temp));
		saveEmitter("particleEmitter.xml");
	}
}

void ParticleFactory::Exit()
{
	//emitter = nullptr;
	/*map<string, Emitter>::const_iterator iter;
	for(iter = emitterMap.begin(); iter != emitterMap.end(); ++iter)
	{
		delete iter.second;
	}*/
	emitterMap.clear();
}

void ParticleFactory::Update(float fElapsedTime)
{
	//emitter.Update(fElapsedTime);
}

void ParticleFactory::Render()
{
	//emitter.Render();
}

bool ParticleFactory::loadEmitter(const char* filename)
{
	TiXmlDocument doc;

	if(doc.LoadFile(filename) == false)
		return false;

	TiXmlElement * pRoot = doc.RootElement();

	if( pRoot == nullptr)
		return false;

	emitterMap.clear();


	TiXmlElement *pEmitter = pRoot->FirstChildElement("Emitter");
	while(pEmitter != nullptr)
	{
		Emitter temp;

		/*const char* pText = pEmitter->GetText();
		if( pText != nullptr)
			strcpy_s(temp.mapName, 20, pText);*/

		double tempD;
		pEmitter->Attribute("posY",				&temp.posY);
		pEmitter->Attribute("posX",				&temp.posX);
		pEmitter->Attribute("height",				&temp.height);
		pEmitter->Attribute("width",				&temp.width);
		int tempShape;
		pEmitter->Attribute("emitterShape",	&tempShape);
		temp.emitterShape = (SpawnShape)tempShape;
		/*pEmitter->Attribute("spread",			&temp.spread);*/
		pEmitter->Attribute("spawnRate",		&temp.spawnRate);
		/*pEmitter->Attribute("direction",			&tempD);
		temp.direction = (float)tempD;*/
		pEmitter->Attribute("AlphaStart",		&tempD);
		temp.AlphaStart = (float)tempD;
		pEmitter->Attribute("RcolorStart",	&tempD);
		temp.RcolorStart = (float)tempD;
		pEmitter->Attribute("GcolorStart",	&tempD);
		temp.GcolorStart = (float)tempD;
		pEmitter->Attribute("BcolorStart",	&tempD);
		temp.BcolorStart = (float)tempD;
		pEmitter->Attribute("AlphaEnd",		&tempD);
		temp.AlphaEnd = (float)tempD;
		pEmitter->Attribute("RcolorEnd",		&tempD);
		temp.RcolorEnd = (float)tempD;
		pEmitter->Attribute("GcolorEnd",		&tempD);
		temp.GcolorEnd = (float)tempD;
		pEmitter->Attribute("BcolorEnd",		&tempD);
		temp.BcolorEnd = (float)tempD;
		pEmitter->Attribute("scaleStart",		&tempD);
		temp.scaleStart = (float)tempD;
		pEmitter->Attribute("scaleEnd",		&tempD);
		temp.scaleEnd = (float)tempD;
		pEmitter->Attribute("velocityYStart",&tempD);
		temp.velocityYStart = (float)tempD;
		pEmitter->Attribute("velocityXStart",&tempD);
		temp.velocityXStart = (float)tempD;
		pEmitter->Attribute("velocityYEnd", &tempD);
		temp.velocityYEnd = (float)tempD;
		pEmitter->Attribute("velocityXEnd", &tempD);
		temp.velocityXEnd = (float)tempD;
		pEmitter->Attribute("lifeMin",			&tempD);
		temp.lifeMin = (float)tempD;
		pEmitter->Attribute("lifeMax",			&tempD);
		temp.lifeMax = (float)tempD;
		pEmitter->Attribute("rotationStart",	&tempD);
		temp.rotationStart = (float)tempD;
		pEmitter->Attribute("rotationRate",	&tempD);
		temp.rotationRate = (float)tempD;
		pEmitter->Attribute("maxParticles",	&temp.maxParticles);

		temp.filepath = pEmitter->Attribute("fileName");
		temp.mapNameID = pEmitter->Attribute("mapName");
	
		const char* tempB;
		tempB = pEmitter->Attribute("looping");
		if(tempB == "false")
			temp.looping = false;
		else
			temp.looping = true;

		emitterMap.insert(std::pair<string, Emitter>(temp.mapNameID, temp));

		pEmitter = pEmitter->NextSiblingElement("Emitter");
	}

	if(emitterMap.size() == 0)
		return false;

	return true;
}

void ParticleFactory::saveEmitter(const char* filenpath)
{
	TiXmlDocument doc;
	TiXmlElement * pRoot = new TiXmlElement("emitter");
	doc.LinkEndChild(pRoot);

	map<string, Emitter>::const_iterator iter;
	for(iter = emitterMap.begin(); iter != emitterMap.end(); ++iter)
	{
		TiXmlElement* pEmitter = new TiXmlElement("emitter_info");
		
		pEmitter->SetAttribute("posY", iter->second.posY);
		pEmitter->SetAttribute("posX", iter->second.posX);
		pEmitter->SetAttribute("height", iter->second.height);
		pEmitter->SetAttribute("width", iter->second.width);
		pEmitter->SetAttribute("emitterShape", iter->second.emitterShape);
		pEmitter->SetAttribute("spread", iter->second.spread);
		pEmitter->SetAttribute("spawnRate", iter->second.spawnRate);
		pEmitter->SetDoubleAttribute("direction", iter->second.direction);
		pEmitter->SetDoubleAttribute("AlphaStart", iter->second.AlphaStart);
		pEmitter->SetDoubleAttribute("RcolorStart", iter->second.RcolorStart);
		pEmitter->SetDoubleAttribute("GcolorStart", iter->second.GcolorStart);
		pEmitter->SetDoubleAttribute("BcolorStart", iter->second.BcolorStart);
		pEmitter->SetDoubleAttribute("AlphaEnd", iter->second.AlphaEnd);
		pEmitter->SetDoubleAttribute("RcolorEnd", iter->second.RcolorEnd);
		pEmitter->SetDoubleAttribute("GcolorEnd", iter->second.GcolorEnd);
		pEmitter->SetDoubleAttribute("BcolorEnd", iter->second.BcolorEnd);
		pEmitter->SetDoubleAttribute("scaleStart", iter->second.scaleStart);
		pEmitter->SetDoubleAttribute("scaleEnd", iter->second.scaleEnd);
		pEmitter->SetDoubleAttribute("velocityYStart", iter->second.velocityYStart);
		pEmitter->SetDoubleAttribute("velocityXStart", iter->second.velocityXStart);
		pEmitter->SetDoubleAttribute("velocityYEnd", iter->second.velocityYEnd);
		pEmitter->SetDoubleAttribute("velocityXEnd", iter->second.velocityXEnd);
		pEmitter->SetDoubleAttribute("lifeMin", iter->second.lifeMin);
		pEmitter->SetDoubleAttribute("lifeMax", iter->second.lifeMax);
		pEmitter->SetDoubleAttribute("rotationStart", iter->second.rotationStart);
		pEmitter->SetDoubleAttribute("rotationRate", iter->second.rotationRate);
		pEmitter->SetAttribute("maxParticles", iter->second.maxParticles);
		pEmitter->SetAttribute("looping", iter->second.looping);

		TiXmlText * pText = new TiXmlText(iter->second.mapName);
		pEmitter->LinkEndChild(pText);

		pRoot->LinkEndChild(pEmitter);

		doc.SaveFile(filenpath);
	}
}

Emitter ParticleFactory::CreateEmitter(string name)
{
	return emitterMap[name];
}
